package com.zhouheng.common.base.enums;

import java.util.Arrays;
import java.util.Optional;

public interface BaseEnum<T> {

    T getValue();

    String getLabel();

    default boolean isEqualValue(T value) {
        return this.getValue().equals(value);
    }

    static <T, R extends BaseEnum> Optional<R> valueOf(Class<R> clzType, T value) {
        BaseEnum[] enums = clzType.getEnumConstants();
        return (Optional<R>) Arrays.stream(enums).filter(v -> v.isEqualValue(value)).findFirst();
    }

    static <T, R extends BaseEnum> Optional<R> nameOf(Class<R> clzType, String name) {
        BaseEnum[] enums = clzType.getEnumConstants();
        return (Optional<R>) Arrays.stream(enums).filter(v -> v.toString().equalsIgnoreCase(name)).findFirst();
    }
}
